(* Implementation du tableau de couleur et de champs d'entrées *)

exception Capacity_exceeded

type color = {
  obj : GMisc.label;
  str : string
}

let clr_default =
  {
    obj = GMisc.label ();
    str = ""
  }

type colorEntryCell = {
  hbox : GPack.box;
  color : color;
  entry : GEdit.entry;
}

let cec_default =
  {
    hbox = GPack.hbox ();
    color = clr_default;
    entry = GEdit.entry ()
  }

type colorEntryList = {
  list : colorEntryCell array;
  mutable size : int;
  capacity : int;
}

let cel_default =
  {
    list = Array.make 0 cec_default;
    size = 0;
    capacity = 0;
  }
    
let is_empty cel =
  cel.size = 0

let sizeofcel cel =
  cel.size
  
let create capacity hbox color str entry =
  {
    list = Array.make capacity {hbox = hbox; color =
        {obj = color; str = str}; entry = entry};
    size = 1;
    capacity = capacity
  }

let insert cel hbox color str entry =
  if cel.size < cel.capacity then
    begin
      cel.list.(cel.size) <- {hbox = hbox; color =
	  {obj = color; str = str}; entry = entry};
      cel.size <- cel.size + 1
    end
  else
    raise Capacity_exceeded

let change_entry_content cel i content =
  if i < cel.size then
    cel.list.(i).entry#set_text content

let generate cel (box:GPack.box) =
  for i = 0 to cel.size - 1 do
    box#pack (cel.list.(i).hbox)#coerce;
    (cel.list.(i).hbox)#pack (cel.list.(i).color.obj)#coerce;
    (cel.list.(i).hbox)#pack (cel.list.(i).entry)#coerce
  done

let delete cel box =
  for i = 0 to cel.size - 1 do
    ignore((cel.list.(i).color.obj)#destroy ());
    ignore((cel.list.(i).entry)#destroy ());
    ignore((cel.list.(i).hbox)#destroy ())
  done;
  cel.size <- 0

let write_alt filename cel =
  let file = open_out filename in
    for i = 0 to cel.size - 1 do
      output_string file (((cel.list.(i).color.str))
                          ^" "
                          ^((cel.list.(i).entry)#text)
                          ^"\n"
                         );
    done;
    close_out file
